package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"online/api/requset"
	"online/intrenal/dao"
	"online/intrenal/model"
)

func AddCollect(c *gin.Context) {
	var req requset.AddCollectReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": nil,
		})
		return
	}
	result := dao.Create(&model.Collect{
		UserId:   c.GetUint("userId"),
		OnlineId: req.OnlineId,
	})
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "收藏商品失败",
			"date": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "收藏商品成功",
		"date": true,
	})
}

func DelCollect(c *gin.Context) {
	var req requset.DelCollectReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": nil,
		})
		return
	}
	result := dao.Delete(&model.Collect{Id: req.Id})
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "收藏商品删除失败",
			"date": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "收藏商品删除成功",
		"date": true,
	})
}

func ListCollect(c *gin.Context) {
	var req requset.ListCollectReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": nil,
		})
		return
	}
	var collect []model.Collect
	result := dao.GetListByFields(&model.Collect{
		Id:       req.Id,
		OnlineId: req.OnlineId,
	}, &collect)
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "收藏商品展示失败",
			"date": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "收藏商品展示成功",
		"date": collect,
	})
}
